<?php

define('E_TEMPLATE','Error: Template %s not found!');

class Template {
  	var $data		= array();
	var $css		= array();
	var $jscript	= array();
	var $jscript2	= array();
	var $head		= array();
		
	function __construct(&$locator) {
		$this->locator 	=& $locator;
		$this->config   =& $this->locator->get('config');
		$this->directory = $this->config->get('config_template');
		$this->set('template',$this->directory);
	}
	
  	function set($key, $value = NULL, $append = FALSE) {
    	if (!is_array($key)) {
    		//if ($key == 'title') { $value .= ' :: ' . $this->config->get('config_store'); $value=htmlentities($value); }
      		if ($append) {
      			$this->data[$key] .= $value;
			} else {
				$this->data[$key] = $value;
			}
    	} else {
    		if ($append) {
	  			$this->data .= array_merge($this->data, $key);
	  		} else {
	  			$this->data = array_merge($this->data, $key);	
	  		}
		}
  	}
  	
  	function get($key) {
    	return $this->data[$key];
  	}

  	function fetch($filename, $directory = DIR_TEMPLATE) {
    	$file = $directory . $this->directory . DS . $filename;
    
    	if (!file_exists($file)) {
    		$file = $directory . DIR_TPL_CORE . DS . $filename;
		}
		
		if (file_exists($file)) {
      		extract($this->data);

      		ob_start();
      
	  		include($file);
      
	  		$content = ob_get_contents();

      		ob_end_clean();

      		return $content;
    	} else {
      		echo sprintf(E_TEMPLATE,$file);
    	}
  	}
  	
  	// Load css from the controller and insert into <head> tag
  	function addCSS($filename, $rel='stylesheet', $media='screen', $directory = DIR_TEMPLATE) {
    
    	$dir = $this->directory;
    	$file = $directory . $dir . DS . $filename;
    	
    	if (!file_exists($file)) {
    		$dir = DIR_TPL_CORE;
    		$file = $directory . $dir . DS . $filename;
		}
		
		if (file_exists($file)) {
			
			$css_data = array(
				'href'  => ((strtolower(APP) == PATH_CATALOG) ? (strtolower(APP).'/') : '') . (PATH_TEMPLATE . '/' . $dir . '/' . $filename),
				'rel'   => $rel,
				'media' => $media
			);
			
			if (in_array(basename($filename), unserialize(HEAD_TO_LOAD_FIRST)) !== false) {
				array_unshift($this->css, $css_data); // Shitty Hack to keep the main jquery.js file on top of the load list.
			} else {
				$this->css[] = $css_data;
			}
			
			$this->css = $this->arrayUnique($this->css);
		}
	}
	
	// Load javascript from the controller and insert into <head> tag
	function addJS($filename, $directory = DIR_APP) {
    
    	$file = $directory . 'javascript' . DS . $filename;
		
		if (file_exists($file)) {
			
			$js_data = array(
				//'src'  => $file
				'src' => ((strtolower(APP) == PATH_CATALOG) ? (strtolower(APP).'/') : '') . ('javascript/' . $filename),
			);
			
			
			if (in_array(basename($filename), unserialize(HEAD_TO_LOAD_FIRST)) !== false) {
				array_unshift($this->jscript, $js_data); // Shitty Hack to keep the main jquery.js file on top of the load list.
			} else {
				$this->jscript[] = $js_data;
			}
			$this->jscript = $this->arrayUnique($this->jscript);
		}
	}
	
	// Load any head tag from the controller and insert into <head> tag
	// Ex: headInject('<link type="text/css" href="catalog/template/default/css/product.css" rel="stylesheet" media="screen">');
	function headInject($content) {
    	
		if ($content) {
			$this->head[] = $content;
			$this->head = $this->arrayUnique($this->head);
		}
	}
	
	// Used by homepage modules, extra images, related products, etc.
	// Automatically determine width, spacing, and imagesize based on config inputs and item count.
	function columnizer($cpr, $itemcount, $padding = 0) {
		
		if (!$cpr || $cpr < 1) { $cpr = 3; } // default to 3 columns if 0 or null
		if ($cpr > $itemcount) { $cpr = $itemcount; }
		
		// if only 4 items, make it 2x2.
		//if ($itemcount == 4 && $cpr != 4) { $cpr = 2; } 
		
		if ($cpr > 1) {
			$colwidth = floor(100/$cpr) - ($padding);
		} else {
			$colwidth = 100;
		}
		
		$rowcount = 0;
		if ($cpr < $itemcount) {
			$rowcount = ceil($itemcount/$cpr);
		}
		
		$output = array(
			'cpr' => $cpr,
			'colwidth' =>  $colwidth,
			'rowcount' => $rowcount
		);
		
		//return array('cpr' => $cpr, 'colwidth' =>  $colwidth, 'rowcount' => $rowcount);
		return $output;
	}
	
	// Get the image from the template image folder or fallback to core
	function getImage($filename, $directory = DIR_TEMPLATE) {
    
		$dir = $this->directory;
    	$file = $directory . $dir . DS . $filename;
    	
    	if (!file_exists($file)) {
    		$dir = DIR_TPL_CORE;
    		$file = $directory . $dir . DS . $filename;
		}

		if (file_exists($file)) {
			return ((strtolower(APP) == PATH_CATALOG) ? strtolower(APP).'/' : '') . (PATH_TEMPLATE . '/' . $dir . '/' . $filename);
		}
	}
	
	// If image button available, use the image, else use a standard button
	// $type is either 'button' or 'submit'. buttons will be replaced with <a> links
	// submits will be replaced with type="image".
	// $text is the button text if there is no image
	function getImageButton($image, $type, $text, $class="", $href="javascript:void(0)", $target = "_self", $directory = DIR_TEMPLATE) {
    
		$dir = $this->directory;
    	$img = $directory . $dir . DS . $image;
        	
    	if (!file_exists($img)) {
    		$dir = DIR_TPL_CORE;
    		$img = $directory . $dir . DS . $image;
		}
		
		$imgsrc = null;
		if (file_exists($img)) {
			$imgsrc = ((strtolower(APP) == PATH_CATALOG) ? strtolower(APP).'/' : '') . (PATH_TEMPLATE . '/' . $dir . '/' . $image);
		}
		
		if ($type == 'button' && isset($imgsrc)) {
			return '<a class="'.$class.'" href="'.$href.'" target="'.$target.'"><img src="'.$imgsrc.'" alt="'.$text.'" /></a>';
		} elseif ($type == 'button' && !isset($imgsrc)) {
			return '<input class="'.$class.'" "type="button" value="'.$text.'" onclick="'.$href.'" />';
		} elseif ($type == 'submit' && isset($imgsrc)) {
			return '<input class="'.$class.'" type="image" src="'.$imgsrc.'" />';
		} else { //$type = 'submit' && !isset($image)
			return '<input class="'.$class.'" type="submit" value="'.$text.'" />';
		}
	}
	
	function arrayUnique($myArray)	{
	    if(!is_array($myArray))
	           return $myArray;

	    foreach ($myArray as &$myvalue){
	        $myvalue=serialize($myvalue);
	    }

	    $myArray=array_unique($myArray);

	    foreach ($myArray as &$myvalue){
	        $myvalue=unserialize($myvalue);
	    }

	    return $myArray;
	} 
}
?>
